package com.service.single.config;

import com.service.single.exception.BizException;
import com.service.single.util.R;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常捕获
 */
@RestControllerAdvice
@Configuration
public class GlobalExceptionHandler {


    @ExceptionHandler(BizException.class)
    public R<String> exceptionHandler(Exception e) {
        return R.faild(e.getMessage());
    }

    /**
     * 参数校验错误
     *
     * @param e
     * @return
     */
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public R<String> argumentValidException(MethodArgumentNotValidException e) {
        return R.faild(e.getBindingResult().getFieldError().getDefaultMessage());
    }

}
